home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / builtin2.zoo / structure.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  5KB  |  178 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* structure.c */
  26.  
  27. #include "builtin.h"
  28.  
  29. static char perm = PERM;
  30.  
  31. b_ARG0()
  32. {    /* (I, T, X) */
  33.         /* I is bound to a number; T is bound to a structure;
  34.         X may be free or bound; no checking for above is done;
  35.         I out of range will cause failure */
  36.  
  37.     register word op1, op2;
  38.     register word *top;
  39.  
  40.         op1 = gregc(1); deref(op1);
  41.         if (!isinteger(op1)) {
  42.         printf("arg0: Index must be an integer.\n");
  43.         Fail0; return;
  44.         }
  45.         op1 = intval(op1);
  46.  
  47.         op2 = gregc(2); deref(op2);
  48.         if (isconstr(op2) && op1 <= get_str_arity(op2) && op1 > 0) 
  49.         if (unify(*(((pw)(untag(op2))) + op1), gregc(3))) return;
  50.         if (islist(op2) && op1 <= 2)
  51.         if (unify(*(((pw)(untag(op2))) + op1 - 1), gregc(3))) return;
  52.         Fail0;
  53. }
  54.  
  55. b_ARITY()
  56. {     /* (T, N) */
  57.         /*  T is bound to a structure or constant; N will be unified with 
  58.         the arity of T */
  59.  
  60.     register word op1, op2;
  61.     register word *top;
  62.  
  63.         op1 = gregc(1);
  64.         deref(op1);
  65.         if (isconstr(op1)) op2 = makeint(get_arity(get_str_psc(op1)));
  66.         else if (islist(op1)) op2 = makeint(2);
  67.         else if (isnum(op1)) op2 = makeint(0);
  68.         else {printf("arity: Term must be bound.\n"); Fail0; return;}
  69.         if (!unify(gregc(2), op2)) {Fail0;} 
  70. }
  71.  
  72. b_FUNCTOR0()
  73. {    /* (T, F) */
  74.         /* T is bound to a structure (no checking), F will be unified
  75.         with the functor of T */
  76.  
  77.     register word op1, op2;
  78.     register pw top;
  79.     struct psc_rec *psc_p;
  80.     word insert();
  81.  
  82.     op1 = gregc(1);
  83.     deref(op1);
  84.     if (islist(op1)) 
  85.     op2 = insert(".", 1, 0, &perm);
  86.     else {
  87.         psc_p = get_str_psc(op1);
  88.     if (get_arity(psc_p) != 0)
  89.         op2 = insert(get_name(psc_p), get_length(psc_p), 0, &perm);
  90.     else op2 = op1;
  91.     }
  92.     op2 |= CS_TAG;
  93.     if (!unify(op2, gregc(2))) {Fail0;}
  94.     
  95. }
  96.  
  97. b_BLDSTR()
  98. {    /* (F, N, T) */
  99.         /* F is bound to an atom, N is bound to an integer, (>0)
  100.            T is free, and will be set to the most general structure
  101.            with functor F; incomplete error checking */
  102.     int i;
  103.     char a;
  104.     struct psc_rec *psc_p;
  105.     register word op1, op2;
  106.     register pw top;
  107.  
  108.     op1 = gregc(1); deref(op1);
  109.     if (!isconstr(op1)) 
  110.     {printf("bldstr: first arg must be constant\n");
  111.     Fail0; return;}
  112.     op2 = gregc(2); deref(op2);
  113.     if (!isinteger(op2))
  114.     {printf("bldstr: second arg must be integer\n");
  115.     Fail0; return;}
  116.     a = intval(op2);
  117.     if ((a < 0) || (a > 127))
  118.     {printf("bldstr: arity must be between 0 and 127\n");
  119.      Fail0; return;}
  120.     op2 = gregc(3);
  121.     deref(op2);
  122.     psc_p = get_str_psc(op1);
  123.     if (get_arity(psc_p)>0) 
  124.     {printf("bldstr: first arg must be constant\n");
  125.     Fail0; return;}
  126.     if ((a==2) && (get_name(psc_p)[0]=='.') && (get_length(psc_p)==1)) 
  127.     follow(op2) = (word)hreg | LIST_TAG;
  128.     else if (a==0) follow(op2) = op1;
  129.     else {
  130.     op1 = insert(get_name(psc_p), get_length(psc_p), a, &perm);
  131.     /* printf("bldstr: %c %d %d %d\n", *get_name(psc_p),
  132.        get_length(psc_p), a, op1); */
  133.     op1 = follow(op1); /* returns addr of addr of psc */
  134.     follow(op2) = (word)hreg | CS_TAG;
  135.     *hreg++ = op1;
  136.     }
  137.     pushtrail(op2);
  138.     for (i = 0; i < a; hreg++, i++) make_free(*hreg);
  139. }
  140.  
  141. b_MKSTR()
  142. {        /*  (F, T, A) : F is a pointer to the PSC table entry of a function
  143.         symbol f/n.  b_MKSTR creates T, a most general instance of
  144.         a term with principal functor f/n on the heap, and sets the
  145.         variable register 2 points to,  to it; the variable that
  146.         register 3 points to is set to the arity n.
  147.  
  148.             At this point, this is used only for decompilation.   */
  149.  
  150.     int i, arity;
  151.     struct psc_rec *psc_p;
  152.     register word op;
  153.     register pw top;
  154.  
  155.     op = gregc(1); deref(op);
  156.     if (!isconstr(op)) {
  157.     printf("mkstr: first arg must be a function symbol!\n");
  158.     Fail0; return; }
  159.     psc_p = (struct psc_rec *)(untagged(op));
  160.     arity = get_arity(psc_p);
  161.     op = gregc(2); deref(op);
  162.     if (!isfree(op)) {
  163.     printf("mkstr: second argument must be a variable!\n");
  164.     Fail0; return; }
  165.     if ((arity==2) && (get_name(psc_p)[0]=='.') && (get_length(psc_p)==1)) 
  166.     follow(op) = (word)hreg | LIST_TAG;
  167.     else {
  168.     follow(op) = (word)hreg | CS_TAG;
  169.     *hreg++ = (word)psc_p;
  170.     if (arity > 0)
  171.         for (i = 0; i < arity; hreg++, i++) make_free(*hreg);
  172.     };
  173.     pushtrail(op);
  174.     op = gregc(3); deref(op);
  175.     follow(op) = makeint(arity);
  176.     pushtrail(op);
  177. }
  178.